package com.timo.redis.util;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @Description: 类的详细说明
 * @Author: timo
 * @Date: 2019/8/26 10:51
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 3616160736427397606L;
    /**
     * 状态码
     */
    protected long code;
    /**
     * 状态信息
     */
    protected String msg;
    /**
     * 状态信息
     */
    private String callback;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 登陆状态
     */
    private Boolean logined;

    /**
     * 请求是否成功，前端根据此字段来判断是否显示msg
     */
    private Boolean isSuccess = true;



    /**
     * 默认构造成功响应
     */
    public Result() {
        this.code = ResultCode.REQUEST_SUCCESS_CODE.code;
        this.msg = ResultCode.REQUEST_SUCCESS_CODE.msg;
        this.callback = null;
        this.data = null;
    }

    public Result(long code, String msg, String callback, T data, Boolean logined, Boolean isSuccess) {
        this.code = code;
        this.msg = msg;
        this.callback = callback;
        this.data = data;
        this.logined = logined;
        this.isSuccess = isSuccess;
    }


    /**
     * ########################
     * #        成功
     * ########################
     */
    public static Result success() {
        return new Result(
                ResultCode.REQUEST_SUCCESS_CODE.code,
                ResultCode.REQUEST_SUCCESS_CODE.msg,
                null,
                null,
                true,
                ResultCode.REQUEST_SUCCESS_CODE.isSuccess
        );
    }

    public static Result success(String msg) {
        return new Result(
                ResultCode.REQUEST_SUCCESS_CODE.code,
                msg,
                null,
                null,
                true,
                ResultCode.REQUEST_SUCCESS_CODE.isSuccess
        );
    }

    public static Result success(Object data) {
        return new Result(
                ResultCode.REQUEST_SUCCESS_CODE.code,
                ResultCode.REQUEST_SUCCESS_CODE.msg,
                null,
                data,
                true,
                ResultCode.REQUEST_SUCCESS_CODE.isSuccess
        );
    }

    public static Result success(Object data, String callback) {
        return new Result(
                ResultCode.REQUEST_SUCCESS_CODE.code,
                ResultCode.REQUEST_SUCCESS_CODE.msg,
                callback,
                data,
                true,
                ResultCode.REQUEST_SUCCESS_CODE.isSuccess
        );
    }

    /**
     * ########################
     * #        失败
     * #########################
     */

    public static Result failure() {
        return new Result(
                ResultCode.REQUEST_FAIL_500_CODE.code,
                ResultCode.REQUEST_FAIL_500_CODE.msg,
                null,
                null,
                true,
                ResultCode.REQUEST_SUCCESS_CODE.isSuccess
        );
    }

    public static Result failure(Long code) {
        ResultCode resultCode = ResultCode.getResultCode(code);
        if (resultCode == null) {
            resultCode = ResultCode.REQUEST_FAIL_000_CODE;
        }
        return new Result(
                resultCode.code,
                resultCode.msg,
                null,
                null,
                true,
                resultCode.isSuccess
        );
    }

    public static Result failure(String msg) {
        ResultCode resultCode = ResultCode.REQUEST_FAIL_500_CODE;
        return new Result(
                resultCode.code,
                msg,
                null,
                null,
                true,
                resultCode.isSuccess
        );
    }


    public static Result failure(HttpStatus httpStatus) {
        return new Result(
                httpStatus.value(),
                httpStatus.name(),
                null,
                null,
                true,
                httpStatus.is2xxSuccessful()
        );
    }


    public static Result failure(HttpStatus httpStatus, String msg) {
        return new Result(
                httpStatus.value(),
                msg,
                null,
                null,
                true,
                httpStatus.is2xxSuccessful()
        );
    }


}
